#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/4/2 15:45
# @Author  : Swper
# @Site    : 
# @File    : models.py
# @Software: PyCharm
from app.exts import db
from datetime import datetime
from werkzeug.security import check_password_hash,generate_password_hash


#文章栏目
class Arctype(db.Model):
    __tablename__ = 'arctype'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)    #编号
    tyname = db.Column(db.String(50),unique=True)        #栏目名
    keywords = db.Column(db.String(100),default='')      #栏目关键字
    addtime = db.Column(db.DateTime,index=True,default=datetime.now)    #添加时间



    def __repr__(self):
        return "<Arctype %r>" %self.tyname

#文章
class Article(db.Model):
    __tablename__ = 'article'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)   #编号
    type_id = db.Column(db.Integer,db.ForeignKey('arctype.id'))  #分类ID
    flag_id = db.Column(db.Integer,db.ForeignKey('arcatt.id'))  #标记名
    title = db.Column(db.String(255),nullable=False)            #文章标题
    writer = db.Column(db.String(50),default='Swper')           #作者
    content = db.Column(db.Text)                                #文章内容
    litpic = db.Column(db.String(255))                          #图片URL
    url = db.Column(db.String(50),nullable=False)               #文章URL
    addtime = db.Column(db.DateTime,index=True,default=datetime.now)  #添加时间
    status = db.Column(db.SmallInteger,default='0')     #状态 ，默认0为正常，1为删除
    keywords = db.Column(db.String(60))                 #关键词
    click = db.Column(db.BigInteger)                    #点击量
    lastpost = db.Column(db.DateTime,default=datetime.now)  #最后更新时间

    tyname = db.relationship('Arctype',backref=db.backref('articles',order_by=id.desc()))
    flag = db.relationship('Arcatt',backref=db.backref('articles'))


    def __repr__(self):
        return "<Article %r>" %self.id


#标记
class Arcatt(db.Model):
    __tablename__ = 'arcatt'
    id = db.Column(db.Integer,primary_key=True)     #编号
    att = db.Column(db.String(4))                   #标记代码 h头条，c推荐，s滚动,p图文
    attname = db.Column(db.String(50))              #中文说明


    def __repr__(self):
        return "<Arcatt %r>" % self.att


#标签
class Tag(db.Model):
    __tablename__ = 'tag'
    id = db.Column(db.Integer,primary_key=True)     #编号
    tname = db.Column(db.String(50),unique=True)    #标签名
    total = db.Column(db.Integer)                   #出现次数

    def __repr__(self):
        return '<Tag %r>' % self.tname


# #用户表
# class User(db.Model):
#     __tablename__ = 'user'
#     id = db.Column(db.Integer,primary_key=True,autoincrement=True)  #编号
#     name = db.Column(db.String(50),unique=True)         #用户名
#     email = db.Column(db.String(255),unique=True)       #邮箱
#     logintime = db.Column(db.DateTime,index=True,default=datetime.now)  #登陆时间
#     loginip = db.Column(db.String(32))      #登陆IP

#rbac权限五张表

class User(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)   #id
    name = db.Column(db.String(50),unique=True)       #用户名
    email = db.Column(db.String(100),unique=True)     #邮箱
    is_admin = db.Column(db.SmallInteger)        #是否管理员，1为是，0 为否
    status = db.Column(db.SmallInteger)          #状态，1为激活，0为禁用
    create_time = db.Column(db.DateTime,index=True,default=datetime.now)    #注册时间
    update_time = db.Column(db.DateTime,index=True,default=datetime.now)    #最后更新时间



class Role(db.Model):
    __tablename__ = 'role'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)   #编号
    name = db.Column(db.String(50),unique=True)                      #权限组
    status = db.Column(db.SmallInteger)                              #状态，1为激活，0为禁用
    create_time = db.Column(db.DateTime,index=True,default=datetime.now)    #添加时间
    update_time = db.Column(db.DateTime,index=True,default=datetime.now)    #最后更新时间

class User_role(db.Model):
    __tablename__ = 'user_role'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)           #编号
    uid = db.Column(db.Integer)      #用户ID
    role_id = db.Column(db.Integer)  #权限ID
    create_time = db.Column(db.DateTime,index=True,default=datetime.now)     #添加时间


class Access(db.Model):
    __tablename__ = 'access'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)        #编号
    title = db.Column(db.String(100))                                     #访问列表
    urls = db.Column(db.String(255))                                      #访问URL地址
    status = db.Column(db.SmallInteger)                                   #状态，1为激活，0为禁用
    create_time = db.Column(db.DateTime,index=True,default=datetime.now)  #添加时间
    update_time = db.Column(db.DateTime,index=True,default=datetime.now)  #最后更新时间


class Role_access(db.Model):
    __tablename__ = 'role_access'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)                #编号
    role_id = db.Column(db.Integer)       #权限ID
    access_id = db.Column(db.Integer) #访问ID
    create_time = db.Column(db.DateTime,index=True,default=datetime.now)          #添加时间




#管理员
class Admin(db.Model):
    __tablename__ = 'admin'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    name = db.Column(db.String(50),nullable=True)   #用户名
    pwd = db.Column(db.String(255),nullable=True)   #密码
    email = db.Column(db.String(100),unique=True)  #邮箱
    status = db.Column(db.String(1),default=1)   #状态
    addtime = db.Column(db.DateTime,index=True,default=datetime.now)   #添加时间
    logintime = db.Column(db.DateTime,index=True,default=datetime.now) #最后登陆时间
    loginip = db.Column(db.String(32),default=None)      #登陆IP


    # 拦截用户密码，进行加密后再存入数据库
    def __init__(self, *args, **kwargs):
        name = kwargs.get('name')
        pwd = kwargs.get('pwd')
        email = kwargs.get('email')
        status = kwargs.get('status')

        self.name = name
        self.email = email
        self.status = status
        self.pwd = generate_password_hash(pwd)

    def check_password(self, raw_password):
        result = check_password_hash(self.pwd, raw_password)
        return result



#文件管理
class Uploads(db.Model):
    __tablename__ = 'uploads'
    id = db.Column(db.Integer,primary_key=True)
    aid = db.Column(db.String(50),nullable=False)
    filename = db.Column(db.String(255))
    url = db.Column(db.String(255),unique=True)
    filesize = db.Column(db.String(255))
    status = db.Column(db.String(1),default='0')
    addtime = db.Column(db.DateTime,index=True,default=datetime.now)

    def __repr__(self):
        return "<Uploads %r>" % self.filename


#友情链接
class Flink(db.Model):
    __tablename__ = 'flink'
    id = db.Column(db.Integer,primary_key=True)
    weburl = db.Column(db.String(255),unique=True)
    webname = db.Column(db.String(50))
    logo = db.Column(db.String(255))
    addtime = db.Column(db.DateTime,index=True,default=datetime.now)

    def __repr__(self):
        return '<Flink %r>' % self.webname


#评论
class Comment(db.Model):
    __tablename__ = 'comment'
    id = db.Column(db.Integer,primary_key=True)
    email = db.Column(db.String(255))
    username = db.Column(db.String(50))
    content = db.Column(db.Text)
    article_id = db.Column(db.Integer,db.ForeignKey('article.id'))
    author_id = db.Column(db.Integer,db.ForeignKey('user.id'))
    addtime = db.Column(db.DateTime,index=True,default=datetime.now)
    loginip = db.Column(db.String(50))

    article = db.relationship('Article',backref=db.backref('comments',order_by=id.desc()))
    author = db.relationship('User',backref=db.backref('comments'))


#网站配置信息
class Settings(db.Model):
    __tablename__ = 'settings'
    id = db.Column(db.Integer,primary_key=True)
    webname = db.Column(db.String(100),default='')
    weburl = db.Column(db.String(100),default='')
    record = db.Column(db.String(100),default='')
    describe = db.Column(db.Text,default='')
    keywords = db.Column(db.String(255),default='')

    def __init__(self, *args, **kwargs):
        webname = kwargs.get('webname')
        weburl = kwargs.get('weburl')
        record = kwargs.get('record')
        describe = kwargs.get('describe')
        keywords = kwargs.get('keywords')

        self.webname = webname
        self.weburl = weburl
        self.record = record
        self.describe = describe
        self.keywords = keywords





















































































































































